package com.example.wod.retrofitdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.wod.retrofitdemo.Bean.GetCapital;
import com.example.wod.retrofitdemo.Bean.Login;
import com.example.wod.retrofitdemo.Bean.Upload;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.button)
    Button button;
    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.button3)
    Button button3;
    @Bind(R.id.button4)
    Button button4;
    @Bind(R.id.button5)
    Button button5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        deleteDatabase("djk");
    }

    @OnClick({R.id.button, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                ApiManage.login("13197709011", "123456", new Callback<Login>() {
                    @Override
                    public void onResponse(Call<Login> call, Response<Login> response) {
                        Log.d("=====", response.body().getInfo());
                    }

                    @Override
                    public void onFailure(Call<Login> call, Throwable t) {

                    }
                });
                break;
            case R.id.button2:
                ApiManage.get(new Callback<GetCapital>() {
                    @Override
                    public void onResponse(Call<GetCapital> call, Response<GetCapital> response) {
                        Log.d("=====", response.body().getInfo());
                    }

                    @Override
                    public void onFailure(Call<GetCapital> call, Throwable t) {

                    }
                });
                break;
            case R.id.button3:
                ApiManage.getinfo("6o5escEKhVI2i3jfolErUQ", new Callback<Login>() {
                    @Override
                    public void onResponse(Call<Login> call, Response<Login> response) {
                        Log.d("=====", response.body().getInfo());
                    }

                    @Override
                    public void onFailure(Call<Login> call, Throwable t) {

                    }
                });
                break;
            case R.id.button4:
                //String path = "file:///android_asset/文件名";
                //MediaType设置,这里传Null就可以了
                //参考
                //http://blog.csdn.net/blueheart20/article/details/45174399


//                String path = "/storage/sdcard0/DCIM/40571204-03d7-4ecf-8e4c-4e9dd3ee2432.jpg";
                String path = "/storage/sdcard0/DCIM/Camera/IMG20160221122706.jpg";
                ApiManage.upload(RequestBody.create(MediaType.parse("image/jpg"), new File(path)), new Callback<Upload>() {
                    @Override
                    public void onResponse(Call<Upload> call, Response<Upload> response) {
                        Log.d("====", response.body().getInfo());
                        if (response.body().getInfo().equals("上传成功！"))
                            Log.d("=====", response.body().getData().getUrl());
                        Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<Upload> call, Throwable t) {

                    }
                });
                break;
            case R.id.button5:
                ApiManage.upload(new File("/storage/sdcard0/DCIM/40571204-03d7-4ecf-8e4c-4e9dd3ee2432.jpg"), new Callback<Upload>() {
                    @Override
                    public void onResponse(Call<Upload> call, Response<Upload> response) {
                        Log.d("====", response.body().getInfo());
                        if (response.body().getInfo().equals("上传成功！"))
                            Log.d("=====", response.body().getData().getUrl());
                    }

                    @Override
                    public void onFailure(Call<Upload> call, Throwable t) {

                    }
                });
                break;
            case R.id.button6:
                Map<String, RequestBody> map = new HashMap<String, RequestBody>();
                File file = new File("/storage/sdcard0/DCIM/40571204-03d7-4ecf-8e4c-4e9dd3ee2432.jpg");
                RequestBody fileBody = RequestBody.create(null, file);
                File file2 = new File("/storage/sdcard0/DCIM/834fe396-583e-4991-86a5-b5717bd63eaf.jpg");
                RequestBody fileBody2 = RequestBody.create(null, file2);
                map.put("image\"; filename=\"" + file.getName() + ".png\"", fileBody);
                map.put("image1\"; filename=\"" + file.getName() + ".jpg\"", fileBody);
                ApiManage.upload(map, new Callback<Upload>() {
                    @Override
                    public void onResponse(Call<Upload> call, Response<Upload> response) {
                        Log.d("====", response.body().getInfo());
//                        if (response.body().getInfo().equals("上传成功！"));
                    }

                    @Override
                    public void onFailure(Call<Upload> call, Throwable t) {

                    }
                });
                break;
        }
    }

    void testokhttp() {
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody.Builder formBody = new FormBody.Builder();
        formBody.add("uname", "13197709011");
        formBody.add("password", "123456");

        final Request request = new Request.Builder()
                .url("http://wyx.wkidt.com/index.php?m=User&c=Public&a=login")
                .post(formBody.build())
                .build();


        okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {

            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                String htmlStr = response.body().string();
                Log.d("====", htmlStr);
            }
        });
    }
}
